package cn.itlaobing.eshopmanagement.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.itlaobing.eshopmanagement.entity.SystemAccount;
import cn.itlaobing.eshopmanagement.service.SessionService;
import cn.itlaobing.eshopmanagement.service.SystemAccountService;

@Controller
@RequestMapping("/systemAccounts")
public class SystemAccountsController extends BaseController{
	
	@Autowired
	private SystemAccountService systemAccountService;
	
	@Autowired
	private SessionService sessionService;

	/**
	 * 通过参数查询
	 * @param model
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public String index(Model model) {
		Map<String, String> map=buildPageRequestSearchFilter();
		model.addAttribute("accounts",systemAccountService.findAll(map));
		return "systemAccounts/index";
	}
	
	/**
	 * 加载新建页面
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.GET)
	public String _new() {
		return "systemAccounts/create";
	}
	
	/**
	 * 创建账户
	 * @param account
	 * @param realName
	 * @return
	 */
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public String create(String account,RedirectAttributes redirectAttributes, String realName) {
		sessionService.create(account, realName);
		redirectAttributes.addFlashAttribute("success", "添加"+account+"成功！");
		return "redirect:/systemAccounts";
	}
	
	/**
	 * ajax 验证账号是否存在
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/account",method=RequestMethod.POST)
	@ResponseBody
	public SystemAccount account(String account) {
		return systemAccountService.selectByPrimaryAccount(account);
	}
	
	/**
	 * 锁定与解锁
	 * @param account
	 * @return
	 */
	@RequestMapping(value="/{id}/lock",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> lock(@PathVariable Long id,Boolean isLock) {
		SystemAccount systemAccount=systemAccountService.selectByPrimaryKey(id);
		Map<String, String> map=new HashMap<>();
		if(systemAccount != null){
			systemAccount.setLocked(isLock);
			int i=systemAccountService.updateByPrimaryKeySelective(systemAccount);
			if(i!=0){
				if(isLock){
					map.put("success", "true");
					map.put("level", "success");
					map.put("message", systemAccount.getAccount()+"已被锁定");
				}else{
					map.put("success", "true");
					map.put("level", "success");
					map.put("message", "解锁"+systemAccount.getAccount()+"成功");
				}
			}else{
				map.put("success", "false");
				map.put("level", "warning");
				map.put("message", "未执行成功");
			}
		}else{
			map.put("success", "false");
			map.put("level", "warning");
			map.put("message", "未执行成功");
		}
		return map;
	}
	
	@RequestMapping(value="/{id}/resetPassowrd",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, String> resetPassowrd(@PathVariable Long id) {
		SystemAccount systemAccount=systemAccountService.selectByPrimaryKey(id);
		Map<String, String> map=new HashMap<>();
		if(systemAccount != null){
			systemAccount.setPasswordDigist("123456");
			sessionService.update(systemAccount);
			map.put("level", "success");
			map.put("message", "重置"+systemAccount.getAccount()+"密码成功，重置后的密码为：123456");
		}else{
			map.put("success", "false");
			map.put("level", "warning");
			map.put("message", "未执行成功");
		}
		return map;
	}
}
